package a日常研究._01网络通信练习;

import java.io.*;
import java.net.Socket;

public class SocketClient {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", 7777);
        OutputStream os = socket.getOutputStream();
        //向server发送消息
        BufferedOutputStream bos = new BufferedOutputStream(os);
        bos.write("hello server！！".getBytes());
        bos.flush();
        System.out.println("发送完毕");
        socket.shutdownOutput();
        //接受server消息
//        InputStream bis = new BufferedInputStream(socket.getInputStream());
        InputStream bis = socket.getInputStream();
        int len;
        byte[] buf = new byte[1024];
        StringBuilder builder = new StringBuilder();
        while ((len = bis.read(buf)) != -1) {
            System.out.println("-=-=-=");
            builder.append(new String(buf, 0, len));
        }
        System.out.println("收到来自服务端的回复：" + builder);
        bos.close();
        bis.close();
        socket.close();
    }
}
